package cn.rocky.pandacodegen.domain.vo;

import cn.rocky.pandacodegen.domain.definition.ColumnDefinition;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class TableDefinition {

    /**
     * 表名
     */
    private String tableName;

    /**
     * 表注释
     */
    private String comment;

    /**
     * Java相关字段
     */
    private transient List<ColumnDefinition> columnDefinitions = Collections.emptyList();


    public void setColumnDefinitions(List<ColumnDefinition> columnDefinitions) {
        this.columnDefinitions = columnDefinitions;
    }

    /**
     * 获取 主键 信息
     *
     * @return {@code ColumnDefinition}
     */
    public ColumnDefinition getPkColumn() {
        ColumnDefinition pk = null;
        for (ColumnDefinition column : columnDefinitions) {
            if ("id".equalsIgnoreCase(column.getColumnName())) {
                pk = column;
            }
            if (column.getIsPk()) {
                return column;
            }
        }
        return pk;
    }
}
